import Mock from 'mockjs'

// 工种分布数据
const jobTypeDistributionData = [
  { jobType: '建筑工人', count: 320 },
  { jobType: '家政服务', count: 280 },
  { jobType: '快递配送', count: 250 },
  { jobType: '餐饮服务', count: 220 },
  { jobType: '保洁服务', count: 190 },
  { jobType: '搬运工人', count: 150 },
  { jobType: '保安服务', count: 140 },
  { jobType: '其他服务', count: 100 }
]

// 生成工资趋势数据
const generateSalaryTrendData = (period) => {
  let days = 30
  switch (period) {
    case '7days':
      days = 7
      break
    case '30days':
      days = 30
      break
    case '3months':
      days = 90
      break
    case '1year':
      days = 365
      break
    default:
      days = 30
  }

  const result = []
  const now = new Date()
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(now.getTime() - i * 24 * 60 * 60 * 1000)
    const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
    
    // 生成基础工资和波动
    const baseValue = 150 + Math.floor(i / 10) * 5 // 基础值随时间增长
    const fluctuation = Math.random() * 20 - 10 // 波动范围 -10 到 10
    
    result.push({
      date: formattedDate,
      averageSalary: Math.round(baseValue + fluctuation)
    })
  }
  
  return result
}

// 工种薪资对比数据
const jobTypeSalaryComparisonData = [
  { jobType: '建筑工人', averageSalary: 210 },
  { jobType: '家政服务', averageSalary: 160 },
  { jobType: '快递配送', averageSalary: 180 },
  { jobType: '餐饮服务', averageSalary: 150 },
  { jobType: '保洁服务', averageSalary: 140 },
  { jobType: '搬运工人', averageSalary: 200 },
  { jobType: '保安服务', averageSalary: 170 },
  { jobType: '其他服务', averageSalary: 165 }
]

// 生成任务成交统计数据
const generateTaskCompletionStats = (range) => {
  let days = 7
  switch (range) {
    case '7days':
      days = 7
      break
    case '30days':
      days = 30
      break
    case 'all':
      days = 60
      break
    default:
      days = 7
  }

  const result = []
  const now = new Date()
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(now.getTime() - i * 24 * 60 * 60 * 1000)
    const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
    
    const publishedCount = Math.floor(Math.random() * 10) + 15
    const completedCount = Math.floor(Math.random() * publishedCount * 0.8)
    const cancelledCount = Math.floor(Math.random() * (publishedCount - completedCount) * 0.5)
    const completionRate = Math.round((completedCount / publishedCount) * 100)
    
    result.push({
      date: formattedDate,
      publishedCount,
      completedCount,
      cancelledCount,
      completionRate
    })
  }
  
  return result
}

// 上传标准选项
const uploadStandardsData = [
  { value: 'gb-t-18859', label: 'GB/T 18859-2017 零工经济数据标准' },
  { value: 'gb-t-35273', label: 'GB/T 35273-2020 就业数据规范' },
  { value: 'gb-t-39725', label: 'GB/T 39725-2018 电子劳务协议规范' },
  { value: 'custom', label: '自定义上传格式' }
]

// 模拟API - 获取工种分布
Mock.mock('/api/analytics/job-type-distribution', 'get', () => {
  return {
    code: 200,
    message: '获取成功',
    data: jobTypeDistributionData
  }
})

// 模拟API - 获取工资趋势
Mock.mock(/\/api\/analytics\/salary-trend(\?.*)?$/, 'get', (options) => {
  // 解析查询参数
  const url = options.url
  const params = new URLSearchParams(url.split('?')[1])
  const period = params.get('period') || '30days'
  
  return {
    code: 200,
    message: '获取成功',
    data: generateSalaryTrendData(period)
  }
})

// 模拟API - 获取工种薪资对比
Mock.mock('/api/analytics/job-type-salary', 'get', () => {
  return {
    code: 200,
    message: '获取成功',
    data: jobTypeSalaryComparisonData
  }
})

// 模拟API - 获取任务成交统计
Mock.mock(/\/api\/analytics\/task-completion(\?.*)?$/, 'get', (options) => {
  // 解析查询参数
  const url = options.url
  const params = new URLSearchParams(url.split('?')[1])
  const range = params.get('range') || '7days'
  
  return {
    code: 200,
    message: '获取成功',
    data: generateTaskCompletionStats(range)
  }
})

// 模拟API - 验证管理员身份
Mock.mock('/api/analytics/verify-admin', 'post', (options) => {
  const { username, password } = JSON.parse(options.body)
  
  // 简单验证逻辑 - 用户名为admin，密码为123456即可通过
  const verified = username === 'admin' && password === '123456'
  
  return {
    code: verified ? 200 : 401,
    message: verified ? '验证成功' : '验证失败',
    data: { verified }
  }
})

// 模拟API - 获取上传标准选项
Mock.mock('/api/analytics/upload-standards', 'get', () => {
  return {
    code: 200,
    message: '获取成功',
    data: uploadStandardsData
  }
})

// 模拟API - 上传数据到政府数据库
Mock.mock('/api/analytics/upload-government', 'post', (options) => {
  const data = JSON.parse(options.body)
  console.log('上传数据:', data)
  
  // 模拟成功场景 (90% 概率)
  const isSuccess = Math.random() > 0.1
  
  if (isSuccess) {
    return {
      code: 200,
      message: '上传成功',
      data: {
        success: true,
        message: '数据上传成功',
        details: [
          `已上传 ${data.startDate} 至 ${data.endDate} 期间的数据`,
          `选择的标准: ${data.standard}`,
          `成功上传 ${Math.floor(Math.random() * 1000) + 500} 条记录`
        ]
      }
    }
  } else {
    return {
      code: 500,
      message: '上传失败',
      data: {
        success: false,
        message: '数据上传失败',
        details: [
          '服务器连接超时',
          '请稍后重试或联系系统管理员'
        ]
      }
    }
  }
})

export default {
  jobTypeDistributionData,
  generateSalaryTrendData,
  jobTypeSalaryComparisonData,
  generateTaskCompletionStats,
  uploadStandardsData
} 